package cn.iune.erp.inventory;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.Utils;
import com.justep.baas.data.Row;
import com.justep.baas.data.RowState;
import com.justep.baas.data.Table;
import com.justep.baas.data.Transform;

@Transactional
@Service
public class StocktakingDetailServiceImpl implements IStocktakingDetailService {
	private final Logger log = LogManager.getLogger(this);

	@Autowired
	private IStocktakingDetailDao dao;
	
	
	public StocktakingDetailServiceImpl(){
//		dao = new StocktakingDetailDaoJPAImpl();
	}
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#getAll()
	 */
	@Override
	public List<StocktakingDetail> getAll(){
		return dao.getAll();
	}
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#getByID(java.lang.String)
	 */
	@Override
	public StocktakingDetail getByID(String fID) {
		return dao.getByID(fID);
	}
	
	@Transactional
	@Override
	public void insert(StocktakingDetail obj) {
		dao.insert(obj);
	}

	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#delete(java.lang.String)
	 */
	@Override
	@Transactional
	public void delete(String fID) {
		dao.delete(fID);
	}
	
	@Transactional
	@Override
	public StocktakingDetail update(StocktakingDetail obj) {
		return dao.update(obj);
	}

	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#getByNo(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public StocktakingDetail getByNo(String recordNo, String serialNo, String enterpriseNo) {
		return dao.getByNo(recordNo, serialNo, enterpriseNo);
	}
	
	@Transactional
	@Override
	public JSONObject save(JSONObject object) throws ParseException {
//		System.out.println("StocktakingDetailServiceImpl。save");
//		System.out.println(object.toJSONString());
		// TODO Auto-generated method stub
		// JSON转换Table
		Table table = Transform.jsonToTable(object);
		
		JSONObject json = new JSONObject();
		json.put("concept", "StocktakingDetail");
		JSONArray rows = new JSONArray();

		List<Row> list = table.getRows();
		for(Row row : list){
			JSONObject javaObject= Transform.rowToJavaObject(table, row);
//			System.out.println(javaObject.toJSONString());
			StocktakingDetail obj = JSON.toJavaObject(javaObject, StocktakingDetail.class);
//			System.out.println("RowState-->"+obj.getRowState());
			if(RowState.NEW.equals(obj.getRowState())){
				dao.insert(obj);
			}
			if(RowState.DELETE.equals(obj.getRowState())){
				dao.delete(obj.getfID());
			}
			if(RowState.EDIT.equals(obj.getRowState())){
				StocktakingDetail objNew = dao.update(obj);
				JSONObject jsonRow = new JSONObject();
				jsonRow.put("id", obj.getfID());
				jsonRow.put("version", objNew.getVersion());
				rows.add(jsonRow);
			}
		}
		
		json.put("rows", rows);
		
		return json;
		
	}


	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#getByMasterID(java.lang.String)
	 */
	@Override
	public List<StocktakingDetail> getByMasterID(String fMasterID) {
		return dao.getByMasterID(fMasterID);
	}
	
	
	///////////////////////业务功能///////////////////////////////////////
	

	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#confirm(java.lang.String)
	 */
	@Transactional
	@Override
	public int confirm(String fID) {
		StocktakingDetail srd = dao.getByID(fID);
		return this.confirm(srd);
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#active(java.lang.String)
	 */
	@Transactional
	@Override
	public int active(String fID) {
		StocktakingDetail srd = dao.getByID(fID);
		return this.active(srd);
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#confirm(com.iune.erp.inventory.StocktakingDetail)
	 */
	@Transactional
	@Override
	public int confirm(StocktakingDetail srd) {
		srd.setState("confirm");
		dao.update(srd);
		//System.out.println("事务测试-->子表提交完成");
		return 0;
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#active(com.iune.erp.inventory.StocktakingDetail)
	 */
	@Transactional
	@Override
	public int active(StocktakingDetail srd) {
		srd.setState("active");
		dao.update(srd);
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#deleteByMasterID(java.lang.String)
	 */
	@Transactional
	@Override
	public int deleteByMasterID(String fMasterID){
		List<StocktakingDetail> srds = dao.getByMasterID(fMasterID);
		for(StocktakingDetail srd:srds){
			dao.delete(srd.getfID());;
		}
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#confirmByMasterID(java.lang.String)
	 */
	@Transactional
	@Override
	public int confirmByMasterID(String fMasterID){
		List<StocktakingDetail> srds = dao.getByMasterID(fMasterID);
		for(StocktakingDetail srd:srds){
			this.confirm(srd);
		}
		return 0;
	}
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#activeByMasterID(java.lang.String)
	 */
	@Transactional
	@Override
	public int activeByMasterID(String fMasterID){
		List<StocktakingDetail> srds = dao.getByMasterID(fMasterID);
		for(StocktakingDetail srd:srds){
			this.active(srd);
		}
		return 0;
	}

	//审核通过，改变审核状态为confirm
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#agreeIt(com.iune.erp.inventory.StocktakingDetail)
	 */
	@Transactional
	@Override
	public StocktakingDetail agreeIt(StocktakingDetail srd) {
		
		srd.setAudit("confirm");
		srd.setConfirmDate(Utils.getCurrentDate());
		
		dao.update(srd);
		//执行更新操作

//		this.doIt(srd);
		
		return srd;
	}
	
	//审核拒绝，改变审核状态为active
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#unAgree(com.iune.erp.inventory.StocktakingDetail)
	 */
	@Transactional
	@Override
	public StocktakingDetail unAgree(StocktakingDetail srd) {

		srd.setAudit("active");
		srd.setConfirmDate(null);
		
		dao.update(srd);
		//执行更新操作
		
//		this.unDo(srd);

		return srd;
	}
	
	//审核通过，改变审核状态为confirm
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#agreeIt(java.lang.String, java.sql.Date)
	 */
	@Transactional
	@Override
	public StocktakingDetail agreeIt(String fID) {
		StocktakingDetail srd = dao.getByID(fID);
		return this.agreeIt(srd);
	}
	
	//审核拒绝，改变审核状态为active
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#unAgree(java.lang.String)
	 */
	@Transactional
	@Override
	public StocktakingDetail unAgree(String fID) {
		StocktakingDetail srd = dao.getByID(fID);
		return this.unAgree(srd);
	}

	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#agreeItByMasterID(java.lang.String)
	 */
	@Transactional
	@Override
	public int agreeItByMasterID(String fMasterID){
		List<StocktakingDetail> srds = dao.getByMasterID(fMasterID);
		for(StocktakingDetail srd :srds){
			this.agreeIt(srd);
		}
		return 0;
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#unAgreeByMasterID(java.lang.String)
	 */
	@Transactional
	@Override
	public int unAgreeByMasterID(String fMasterID){
		List<StocktakingDetail> srds = dao.getByMasterID(fMasterID);
		for(StocktakingDetail srd :srds){
			this.unAgree(srd);
		}
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#agreeIt(java.util.List, java.sql.Date)
	 */
	@Transactional
	@Override
	public int agreeIt(List<StocktakingDetail> srds){
		for(StocktakingDetail srd :srds){
			this.agreeIt(srd);
		}
		return 0;
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#unAgree(java.util.List)
	 */
	@Transactional
	@Override
	public int unAgree(List<StocktakingDetail> srds){
		for(StocktakingDetail srd :srds){
			this.unAgree(srd);
		}
		return 0;
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#open(java.lang.String)
	 */
	@Transactional
	@Override
	public int open(String fID) {
		StocktakingDetail srd = dao.getByID(fID);
		return this.open(srd);
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#close(java.lang.String)
	 */
	@Transactional
	@Override
	public int close(String fID) {
		StocktakingDetail srd = dao.getByID(fID);
		return this.close(srd);
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#open(com.iune.erp.inventory.StocktakingDetail)
	 */
	@Transactional
	@Override
	public int open(StocktakingDetail srd) {
		srd.setStatus("open");
		dao.update(srd);
		return 0;
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#close(com.iune.erp.inventory.StocktakingDetail)
	 */
	@Transactional
	@Override
	public int close(StocktakingDetail srd) {
		srd.setStatus("close");
		dao.update(srd);
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#openByMasterID(java.lang.String)
	 */
	@Transactional
	@Override
	public int openByMasterID(String fMasterID){
		List<StocktakingDetail> srds = dao.getByMasterID(fMasterID);
		for(StocktakingDetail srd:srds){
			this.open(srd);
		}
		return 0;
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStocktakingDetailService#closeByMasterID(java.lang.String)
	 */
	@Transactional
	@Override
	public int closeByMasterID(String fMasterID){
		List<StocktakingDetail> srds = dao.getByMasterID(fMasterID);
		for(StocktakingDetail srd:srds){
			this.close(srd);
		}
		return 0;
	}
	
	

	@Override
	public StocktakingDetail packing(Stocktaking stocktaking){
		StocktakingDetail obj = new StocktakingDetail();
		String fID = Utils.getUUID();
		obj.setfID(fID);
		
		obj.setBillType(stocktaking.getBillType());
		obj.setBillTypeName(stocktaking.getBillTypeName());
		obj.setSystemCode(stocktaking.getSystemCode());
		
		obj.setState(stocktaking.getState());
		obj.setStatus(stocktaking.getStatus());
		obj.setAudit(stocktaking.getAudit());
		obj.setOrderDate(stocktaking.getOrderDate());
		
		obj.setfMasterID(stocktaking.getfID());
		obj.setDocumentNo(stocktaking.getDocumentNo());
		
		//子表明细号
		int maxDetailNo = stocktaking.getMaxDetailNo() + 1;
		String serialNo = String.format("%05d", maxDetailNo);
		obj.setSerialNo(serialNo);
		
		obj.setBatchFlag(0);
		
		//账面数量
		BigDecimal bookQuantity = BigDecimal.ZERO;
		//盘点数量
		BigDecimal takeQuantity = BigDecimal.ZERO;
		//账面包装数量
		BigDecimal bookPackage = BigDecimal.ZERO;
		//盘点包装数量
		BigDecimal takePackage = BigDecimal.ZERO;
		
		obj.setBookQuantity(bookQuantity);
		obj.setTakeQuantity(takeQuantity);
		obj.setBookPackage(bookPackage);
		obj.setTakePackage(takePackage);
		
		obj.setEmployeeNo(stocktaking.getEmployeeNo());
		obj.setEmployeeName(stocktaking.getEmployeeName());
		
		obj.setUserId(stocktaking.getUserId());
		obj.setEnterpriseNo(stocktaking.getEnterpriseNo());
		obj.setEnterpriseName(stocktaking.getEnterpriseName());
		
		obj.setVersion(0);
		
		return obj;
	}
	
	
	
	
	////////////////////做为主表时使用//////////////////////////////////////
	
	@Override
	public JSONObject newData(JSONObject data) {
		return dao.create(data);
	}
	
	@Override
	public JSONObject deleteData(JSONObject param) {
//		String ucode = param.getString("ucode");
//		String uname = param.getString("uname");
//		String uid = param.getString("uid");
		
		JSONArray ids = param.getJSONArray("ids");
		for(int i=0;i<ids.size();i++){
			String id = ids.getString(i);
			//这里可以增加是否可删除的判断

			//然后删除主表
			dao.delete(id);
		}

		JSONObject data = new JSONObject();
		data.put("ids", ids);
		
		JSONObject json = new JSONObject();
		json.put("code", 0);
		json.put("msg", "success");
		json.put("data", data);
		
		return json;
	}

	@Override
	public JSONObject saveData(JSONObject param) {
		// TODO Auto-generated method stub
		JSONObject data;
		try {
			//保存主表
			data = this.save(param);
					
			JSONObject json = new JSONObject();
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", data);
			
			return json;
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			log.info("开始打印ParseException");
			e.printStackTrace();
			log.info("e.getMessage()-->>"+e.getMessage());
			log.info("完成打印ParseException");
			
			JSONObject json = new JSONObject();
			json.put("code", -1);
			json.put("msg", e.getMessage());
			
			return json;
		} 
	}

	@Override
	public JSONObject queryData(JSONObject param) {
		// TODO Auto-generated method stub
		try {
			JSONObject jsonTable = dao.query(param);
			
			JSONObject json = new JSONObject();
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", jsonTable);
			
			return json;
		} catch (SQLException e) {
			e.printStackTrace();
			
			JSONObject json = new JSONObject();
			json.put("code", -1);
			json.put("msg", e.getMessage());
			return json;
		}
	}
	@Override
	public JSONObject validData(JSONObject param) {
		// TODO Auto-generated method stub
		return null;
	}

	////////////////////做为主表时使用//////////////////////////////////////

	
	
	
	
	
}
